<?php

namespace app\common\controller;

use think\Exception;
class ApiBackend extends CommonBase
{
    // 模型
    protected $model;
    // 服务
    protected $service;
    // 校验
    protected $validate;
    // 站长ID
    protected $adminId;
    // 站长登录信息
    protected $adminInfo;
    // 用户ID
    protected $userId;
    // 用户登录信息
    protected $userInfo;
    // 网络请求
    protected $param;
    /**
     * @var \app\admin\model\AppModel|array|false|\think\Model
     */
    protected $myAppInfo;
    protected $myPowerInfo;

    /**
     * 初始化
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

    }

    public function userLogin(){
        try{
            $userId = cookie('userId');
            $sign = cookie('userSign');
            $this->userId = $userId;

            // 登录用户信息
            $userModel = new \app\user\model\User();
            $userInfo = $userModel->getInfo();
             if(!empty($userId) && $sign != data_auth_sign($userInfo['appid'].$userInfo['username'].$userInfo['password'].sf_password_hash())){
                throw new Exception('用户登录信息已过期，请刷新界面重新登录！');
            }
            if(!$userInfo){
                throw new Exception('您的账号异常，请联系站长处理！[errorCode:UserInfoError]');
            }
            $this->userInfo = $userInfo;
            if(empty($userInfo['appid'])){
                throw new Exception('您的账号异常，请联系站长处理！[errorCode:UserAppIdEmpty]');
            }
            if(!is_numeric($userInfo['appid']) || $userInfo['appid'] <= 0){
                throw new Exception('您的账号异常，请联系站长处理！[errorCode:UserAppIdError]');
            }
            if($userInfo['status'] != 1){
                throw new Exception('您的账号已被封禁，请联系上级处理！[errorCode:UserStatusBlocked]');
            }
            if(!empty($userInfo['ip'])){
                if(!in_array(get_client_ip(),unserialize($userInfo['ip']))){
                    throw new Exception('您当前的IP不在白名单内，请返回重新登录！');
                }
            }
            $appModel = new \app\admin\model\AppModel();
            $this->myAppInfo = $appModel->getInfo(intval($userInfo['appid']));
            if(!$this->myAppInfo){
                throw new Exception('您的账号异常，请联系站长处理！[errorCode:GetUserAppInfoError]');
            }
            $this->myPowerInfo = $this->getPowerPriceInfo(intval($userInfo['power']));
            if(!$this->myPowerInfo){
                throw new Exception('您的账号异常，请联系站长处理！[errorCode:GetUserPowerInfoError]');
            }
            // 数据绑定
        }catch (\Exception $e){
            //$this->logOut();
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 获取权限信息
     * @return array
     * @author 陌上花开
     * @date 2022/1/25
     */
    private function getPowerPriceInfo($id)
    {
        if(empty($id)) return false;
        $powerPriceModel = new \app\admin\model\PowerPriceModel();
        try{
            $result = $powerPriceModel->getInfo($id);
            if(!$result) return false;
            return $result;
        }catch (\Exception $e){
            return false;
        }

    }

    public function logOut(){
        cookie('userId',null);
        cookie('userSign',null);
        return message('退出登录成功', true);
    }
}